package com.education.project.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.education.project.model.WebNavResources;
import com.education.project.mapper.WebNavResourcesMapper;
import com.education.project.service.WebNavResourcesService;

@Service
public class WebNavResourcesServiceImpl implements WebNavResourcesService {
	@Autowired
	private WebNavResourcesMapper webNavResourcesMapper;
	
	@Override
	public List getResourcesByUserType(Integer loginUserTypeId) {
		List<WebNavResources> roots = webNavResourcesMapper.getResourcesByUserType(loginUserTypeId, 0);
		if(roots.size()>0) {
			for(WebNavResources nav:roots) {
				List<WebNavResources> children = webNavResourcesMapper.getResourcesByUserType(loginUserTypeId, nav.getId());
				nav.setChildren(children);
			}
		}
		return roots;
	}
	
	@Override
	public List getResourcesByNode() {
		List<WebNavResources> roots = webNavResourcesMapper.getResourcesByNode(0);
		if(roots.size()>0) {
			for(WebNavResources nav:roots) {
				List<WebNavResources> children = webNavResourcesMapper.getResourcesByNode(nav.getId());
				nav.setChildren(children);
			}
		}
		return roots;
	}

	@Override
	public List getResourcesByUserTypeTree(Integer loginUserTypeId) {
		List<WebNavResources> all = webNavResourcesMapper.getResourcesByUserTypeAll(loginUserTypeId);
		List<WebNavResources> roots = webNavResourcesMapper.getResourcesByNode(0);
		List<Map> list1 = new ArrayList<Map>();
		if(roots.size()>0) {
			for(WebNavResources nav:roots) {
				Map root = new HashMap();
				root.put("id", nav.getId());
				root.put("text", nav.getName());
				WebNavResources find = findWebNavResources(nav.getId(), all);
				boolean checked = false;
				if(find!=null) {
					checked = true;
				} 
				int childCnt = 0;
				List<WebNavResources> children = webNavResourcesMapper.getResourcesByNode(nav.getId());
				if(children.size()>0) {
					List<Map> list2 = new ArrayList<Map>();
					for(WebNavResources child:children) {
						Map node = new HashMap();
						node.put("id", child.getId());
						node.put("text", child.getName());
						find = findWebNavResources(child.getId(), all);
						if(find!=null) {
							node.put("checked", true);
							childCnt++;
						} 
						list2.add(node);
					}
					root.put("children", list2);
				}
				if(childCnt>0) {
					checked = false;
				}
				root.put("checked", checked);
				list1.add(root);
			}
		}
		return list1;
	}
	
	private WebNavResources findWebNavResources(Integer navId, List<WebNavResources> list) {
		int findIndex = -1;
		for(int i=0;i<list.size();i++) {
			WebNavResources nav = list.get(i);
			if(nav.getId().intValue()==navId.intValue()) {
				findIndex = i;
				break;
			}
		}
		if(findIndex>=0) {
			return list.get(findIndex);
		}
		return null;
	}

	@Override
	public List getResourcesByRoot() {
		return webNavResourcesMapper.getResourcesByNode(0);
	}

	@Override
	public WebNavResources getWebNavResources(Integer id) {
		return webNavResourcesMapper.get(id);
	}

	@Override
	public Integer getChildrenNavCount(Integer parentid) {
		return webNavResourcesMapper.getChildrenCount(parentid);
	}
	
	@Override
	public Integer getNavResourcesCountByNav(Integer id) {
		return webNavResourcesMapper.getNavResourcesCountByNav(id);
	}
	
	@Override
	@Transactional
	public void deleteWebNavResources(Integer id) {
		webNavResourcesMapper.delete(id);
	}
	
	@Override
	@Transactional
	public void updateWebNavResources(WebNavResources webNavResources) {
		webNavResourcesMapper.update(webNavResources);
	}

	@Override
	@Transactional
	public void saveWebNavResources(WebNavResources webNavResources) {
		webNavResourcesMapper.save(webNavResources);
	}

	@Override
	public Integer getNavResourcesCountByUserType(Integer usertypeid) {
		return webNavResourcesMapper.getNavResourcesCountByUserType(usertypeid);
	}
}
